function AllReady() {
    var mstform = Ext.getCmp('CntM1');//获取容器
    var CntD1 = Ext.getCmp('CntD1');//获取容器
    var toolbar = Ext.getCmp('toolbar');
    var tabPanel = Ext.getCmp('tabPanel');

    var PhidDept = mstform.queryById('PhidDept');//获取字段
    var user_product_line = mstform.queryById('user_product_line');
    var PhidPc = mstform.queryById('PhidPc');
    var BRPcNo = mstform.queryById('BRPcNo');
    var PhidYsfl = mstform.queryById('PhidYsfl');

    if (otype == 'add') {
        PhidYsfl.setValue('14')
        BatchBindCombox([PhidYsfl])

        if (PhidPc.getValue()>'0') {
            execServer('getProjInfo', { 'pc': PhidPc.getValue() }, function (res) {
                if (res.count > 0) {
                    var obj = JSON.parse(res.data);
                    PhidDept.setValue(obj[0].extendObjects.PhidDept)
                    user_product_line.setValue(obj[0].extendObjects.cpx)
                    BRPcNo.setValue(obj[0].extendObjects.pc_no)

                    BatchBindCombox([PhidDept])
                    BatchBindCombox([user_product_line])
                }
            });
        }

    }


    PhidPc.addListener('helpselected', function () {
        execServer('getProjInfo', { 'pc': PhidPc.getValue() }, function (res) {
            if (res.count > 0) {
                var obj = JSON.parse(res.data);
                PhidDept.setValue(obj[0].extendObjects.PhidDept)
                user_product_line.setValue(obj[0].extendObjects.cpx)
                BRPcNo.setValue(obj[0].extendObjects.pc_no)

                BatchBindCombox([PhidDept])
                BatchBindCombox([user_product_line])
            }
        });

    });

    CntD1.setReadOnlyCol('Prc', false);

    addTabs(mstform, tabPanel, toolbar);
    var cnt_hk_plan = Ext.getCmp('cnt_hk_plan');
    var cnt_hk_plan_store = cnt_hk_plan.getStore();

    if (otype == 'edit' || otype == 'view') {
        execServer('GetPaymentPlanInfo', { ht: busid }, function (res) {
            console.log(res);  // 打印整个响应对象以便调试

            if (res && res.status === 'success' && res.data) {
                try {
                    // 将data字符串解析为JavaScript对象
                    const parsedData = JSON.parse(res.data);

                    if (Array.isArray(parsedData)) {
                        var arr = [];
                        for (let i = 0; i < res.count; i++) {
                            const record = parsedData[i].extendObjects;  // 从extendObjects中获取数据
                            arr.push({
                                hklcb: record.u_hklcb,
                                hkrq: record.u_hk_dt,
                                hkzq: record.u_hkzq,
                                hkbl: record.u_hkbl * 100 + '%',
                                hkje: record.u_hk_amt,
                                bb_amt: record.u_bb_amt,
                                rmb_amt: record.u_rmb_amt,
                                remarks: record.u_remarks,
                            });
                        }
                        // 插入数据到存储
                        cnt_hk_plan_store.insert(cnt_hk_plan_store.getCount(), arr);
                    } else {
                        console.error("Unexpected data format, expected an array:", parsedData);
                    }
                } catch (error) {
                    console.error("Error parsing JSON:", error);
                }
            } else {
                console.error("Unexpected response format or status:", res);
            }
        });
    }

}
function generateId(sequence) {
    //获取当前时间
    const date = new Date();
    // 格式化日期为 YYMMDD
    const year = date.getFullYear().toString().slice(-2);
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const dateStr = `${year}${month}${day}`;
    // 格式化时间为 HHMMSS
    const hours = date.getHours().toString().padStart(2, '0');
    const minutes = date.getMinutes().toString().padStart(2, '0');
    const seconds = date.getSeconds().toString().padStart(2, '0');
    const timeStr = `${hours}${minutes}${seconds}`;
    // 获取毫秒数，并确保是2位数
    const milliseconds = date.getMilliseconds().toString().padStart(3, '0').slice(0, 2);
    // 格式化流水号为3位
    const sequenceStr = sequence.toString().padStart(3, '0');
    // 拼接日期、时间、毫秒和流水号
    const phid = `${dateStr}${timeStr}${milliseconds}${sequenceStr}`;
    return phid;
}
function addTabs(mstform, tabPanel, toolbar) {

    //定义model --收款计划
    var cnt_hk_plan_model = Ext.define('JdNextPlanmodel', {
        extend: 'Ext.data.Model',
        fields: [{
            name: 'phid',
            type: 'bigInt',
            mapping: 'phid'
        }, {
            name: 'mstphid',
            type: 'bigInt',
            mapping: 'mstphid'
        },
        {
            name: 'hklcb',
            type: 'string',
            mapping: 'hklcb'
        }, {
            name: 'hkrq',
            type: 'string',
            mapping: 'hkrq'
        },
        {
            name: 'hkzq',
            type: 'string',
            mapping: 'hkzq'
        }, {
            name: 'hkbl',
            type: 'string',
            mapping: 'hkbl'
        }, {
            name: 'hkje',
            type: 'string',
            mapping: 'hkje'
        }, {
            name: 'bb_amt',
            type: 'string',
            mapping: 'bb_amt'
        }, {
            name: 'rmb_amt',
            type: 'string',
            mapping: 'rmb_amt'
        },
        {
            name: 'remarks',
            type: 'string',
            mapping: 'remarks'
        },
        ]
    });

    //定义store
    var cnt_hk_plan_store = Ext.create('Ext.ng.JsonStore', {
        model: cnt_hk_plan_model
    });
    //合同信息 grid  
    var cnt_hk_plan = Ext.create('Ext.ng.GridPanel', {
        title: '收款计划',
        id: 'cnt_hk_plan',
        region: 'south',
        stateful: false,
        isInit: true,
        MaxHeight: 800,
        stateId: 'cnt_hk_plan', //所有单据不允许重复
        store: cnt_hk_plan_store,
        buskey: 'phid', //对应的业务表主键属性
        otype: otype,
        columnLines: true,
        tbar: [{
            xtype: 'button',
            itemId: 'addrow',
            text: '增行',
            iconCls: "icon-AddRow",
            handler: function () {
                var newRecord = {
                    'phid': generateId(cnt_hk_plan_store.getCount() + 1).toString(),
                    // 可以在这里添加其他初始字段
                };
                cnt_hk_plan_store.insert(cnt_hk_plan_store.getCount(), [newRecord]);
            }
        }, {
            xtype: 'button',
            itemId: 'delrow',
            text: '删行',
            iconCls: "icon-DeleteRow",
            handler: function () {
                var data = cnt_hk_plan.getSelectionModel().getSelection();
                Ext.Array.each(data, function (record) {
                    cnt_hk_plan_store.remove(record);
                });
            }
        }
        ],
        columns: [Ext.create('Ext.grid.RowNumberer', { text: '序号', width: 45 }),
        {
            header: Lang.pcm3_cnt_m_phid || '主键',
            dataIndex: 'phid',
            itemId: 'phid',
            width: 100,
            sortable: true,
            hidden: true
        },
        {
            header: Lang.pcm3_cnt_m_phid || '单据主键',
            dataIndex: 'mstphid',
            itemId: 'mstphid',
            width: 100,
            sortable: true,
            hidden: true,
            readOnly: false,
            editor: {
                xtype: 'ngText',
                allowBlank: true
            }
        }, {
            header: '回款里程碑',
            dataIndex: 'hklcb',
            itemId: 'hklcb',
            readOnly: false,
            mustInput: false,
            width: 100,
            sortable: true,
            locked: false,
            hidden: false,
            editor: {
                xtype: 'ngText',
                allowBlank: true
            }
        },
        {
            header: '回款日期',
            dataIndex: 'hkrq',
            itemId: 'hkrq',
            readOnly: false,
            mustInput: false,
            width: 100,
            sortable: true,
            locked: false,
            hidden: false,
            editor: {
                xtype: 'ngDateTime',
                allowBlank: true
            }
        }, {
            header: '回款周期(合同签订后天数)',
            dataIndex: 'hkzq',
            itemId: 'hkzq',
            readOnly: false,
            mustInput: false,
            width: 300,
            sortable: true,
            locked: false,
            hidden: false,
            editor: {
                xtype: 'ngText',
                allowBlank: true
            }
        }, {
            header: '回款比例%',
            dataIndex: 'hkbl',
            itemId: 'hkbl',
            readOnly: false,
            mustInput: false,
            width: 200,
            sortable: true,
            locked: false,
            hidden: false,
            editor: {
                xtype: 'ngText',
                allowBlank: true
            }
        }, {
            header: '回款金额',
            dataIndex: 'hkje',
            itemId: 'hkje',
            readOnly: false,
            mustInput: false,
            width: 100,
            sortable: true,
            locked: false,
            hidden: false,
            editor: {
                xtype: 'ngNumber',
                allowBlank: true
            }, align: 'right',
            mustInput: false,
            hidden: false,
            renderer: function (val) {
                return Ext.util.Format.number(val, '000,000,000,000.00');
            }
        }, {
            header: '本币金额',
            dataIndex: 'bb_amt',
            itemId: 'bb_amt',
            readOnly: false,
            mustInput: false,
            width: 100,
            sortable: true,
            locked: false,
            hidden: false,
            editor: {
                xtype: 'ngNumber',
                allowBlank: true
            }, align: 'right',
            mustInput: false,
            hidden: false,
            renderer: function (val) {
                return Ext.util.Format.number(val, '000,000,000,000.00');
            }
        }, {
            header: '人民币金额',
            dataIndex: 'rmb_amt',
            itemId: 'rmb_amt',
            readOnly: false,
            mustInput: false,
            width: 100,
            sortable: true,
            locked: false,
            hidden: false,
            editor: {
                xtype: 'ngNumber',
                allowBlank: true
            }, align: 'right',
            mustInput: false,
            hidden: false,
            renderer: function (val) {
                return Ext.util.Format.number(val, '000,000,000,000.00');
            }
        }, {
            header: '备注',
            dataIndex: 'remarks',
            itemId: 'remarks',
            readOnly: false,
            mustInput: false,
            width: 500,
            sortable: true,
            locked: false,
            hidden: false,
            editor: {
                xtype: 'ngText',
                allowBlank: true
            }
        },
        ],
        plugins: [
            Ext.create('Ext.grid.plugin.CellEditing', {
                clicksToEdit: 1
            })
        ]
    });
    tabPanel.add(cnt_hk_plan);
    /* if (cnt_hk_planStore.sorters.length == '0') {
         var arr = new Array();
         for (var i = 0; i < 1; i++) {
             arr.push({
                 phid: generateId(cnt_hk_planStore.getCount() + 1).toString(),
 
             })
         }
         cnt_hk_planStore.removeAll(); //清除单据体内所有数据
         cnt_hk_planStore.insert(cnt_hk_planStore.getCount(), arr); //将服务端获取的数组内容插入到单据体
     }*/

}


function getExtendData() {

    var cnt_hk_plan = Ext.getCmp('cnt_hk_plan');
    var extendObj2 = cnt_hk_plan.getChange(false);

    //手动建立的表名
    return {
        'cnt_hk_plan': JSON.stringify(extendObj2),
    };

}

